package com.hiultra.archive.service.impl;

import com.hiultra.archive.domain.ArcAlarm;
import com.hiultra.archive.domain.ArcBox;
import com.hiultra.archive.mapper.ArcArchiveStatisticseMapper;
import com.hiultra.archive.mapper.ArcCatalogMapper;
import com.hiultra.archive.mapper.RoomArchiveMapper;
import com.hiultra.archive.service.IArcArchiveStatisticseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class IArcArchiveStatisticseServiceImpl implements IArcArchiveStatisticseService {
    @Autowired
    private ArcCatalogMapper arcCatalogMapper;

    @Autowired
    private ArcArchiveStatisticseMapper arcArchiveStatisticseMapper;

    @Autowired
    private RoomArchiveMapper roomArchiveMapper;

    /**
     * 档案分类数量统计
     *
     * @return
     */
    @Override
    public Map<String, Object> arcCatalogNumber() {
        Map<String, Object> map = new HashMap<String, Object>();
        List<Map<String, Object>> li = new ArrayList<Map<String, Object>>();
        // li = arcArchiveMapper.selectCatalogNum(entity);

        List<String> catalogNameli = new ArrayList<String>();
        // 查询一级分类
        List<String> typeList = arcArchiveStatisticseMapper.selectTypeNameByType();
        for (String type : typeList) {
            String name = "";
            // 根据code模糊查询到所有的数量
            int arcNumbyCatalog = arcArchiveStatisticseMapper.selectNumByType(type);
            if (arcNumbyCatalog > 0) {
                Map<String, Object> catalogData = new HashMap<String, Object>();
                if (("01").equals(type)) {
                    catalogNameli.add("高压居民");
                    name = "高压居民";
                } else if (("02").equals(type)) {
                    catalogNameli.add("低压非居民");
                    name = "低压非居民";
                } else if (("03").equals(type)) {
                    catalogNameli.add("低压居民");
                    name = "低压居民";
                } else if (("04").equals(type)) {
                    catalogNameli.add("重要用户");
                    name = "重要用户";
                }

                catalogData.put("value", arcNumbyCatalog);
                catalogData.put("name", name);
                li.add(catalogData);
            }

        }

        String[] catalogArr = catalogNameli.toArray(new String[catalogNameli.size()]);
        for (String a: catalogArr
             ) {
            System.out.println(a);
        }

//		for (int i = 0; i < li.size(); i++) {
//			Map<String, Object> mapT = li.get(i);
//			String depmName = (String) mapT.get("name");
//			catalogArr[i] = depmName;
//		}

        map.put("catalogList", li);
        map.put("catalogArr", catalogArr);

        return map;
    }

    /**
     * 智能柜数量统计
     **/
    @Override
    public Map<String, Object> cabinetNumberMap() {
        // x轴智能柜名称
        ArrayList<String> getShelvesName = arcArchiveStatisticseMapper.queryShelvesName();
        ArrayList<String> getShelvesCode = arcArchiveStatisticseMapper.queryShelvesCode();
        String[] boxNumArr = new String[getShelvesCode.size()];
        String[] archiveNumArr = new String[getShelvesCode.size()];
        ArcBox box = new ArcBox();
        for (int i = 0; i < getShelvesCode.size(); i++) {

            String boxNum = arcArchiveStatisticseMapper.selectBoxNum(box);
            String archiveNum = arcArchiveStatisticseMapper.selectCabinetArcarchiveNum(box);
            boxNumArr[i] = boxNum;
            archiveNumArr[i] = archiveNum;
        }

        Map<String, Object> map = new HashMap<String, Object>();

        map.put("getCabinetName", getShelvesName);
        map.put("boxNumArr", boxNumArr);
        map.put("archiveNumArr", archiveNumArr);
        return map;
    }

    /**
     * 查询倒叙20条预警列表
     */
    @Override
    public List<Map<String, ArcAlarm>> selectListDescLimit() {
        return arcArchiveStatisticseMapper.selectListDescLimit();
    }

    /**
     * 档案年增加数量和年利用率
     **/
    @Override
    public Map<String, Object> archiveAddNumberYear() {
        // x轴年份
        ArrayList<String> yearData = arcArchiveStatisticseMapper.queryArchiveYear();
        String oneYear = yearData.get(0);
        String lastYear = yearData.get(6);
        oneYear = new String(Integer.parseInt(oneYear) - 1 + "");
        lastYear = new String(Integer.parseInt(lastYear) + 1 + "");
        String delflag = new String("0");
        String isUse = new String("0");
        // 年增加档案
        ArrayList<Integer> yearDataNum = arcArchiveStatisticseMapper.queryArchiveAddNumYear(oneYear, lastYear, delflag, null);
        // 年利用档案
        isUse = "1";
        ArrayList<Integer> yearDataNumUse = arcArchiveStatisticseMapper.queryArchiveAddNumYear(oneYear, lastYear, delflag, isUse);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("yearData", yearData);
        map.put("yearDataNumAdd", yearDataNum);
        map.put("yearDataNumUse", yearDataNumUse);
        return map;
    }

    @Override
    public Map<String, Object> archiveAddNumberMouth() {
        Map<String, Object> map = new HashMap<String, Object>();
        // 月新增档案
        String isUse = new String("0");
        ArrayList<Integer> archiveAddNumberMouth = arcArchiveStatisticseMapper.archiveAddNumberMouth(isUse);
        //月利用
        isUse = "1";
        ArrayList<Integer> archiveUseNumberMouth = arcArchiveStatisticseMapper.archiveAddNumberMouth(isUse);
        map.put("addNumberMouth", archiveAddNumberMouth);
        map.put("useNumberMouth", archiveUseNumberMouth);
        return map;
    }

    @Override
    public Map<String, String> selectNum() {
        Map map = new HashMap<>();
        //档案总数
        Integer archiveNum = arcArchiveStatisticseMapper.setArchiveNum();
        //盒总数
        Integer boxNum = arcArchiveStatisticseMapper.setboxNum();
        //在柜盒数
        Integer inBoxNum = arcArchiveStatisticseMapper.setboxNumCabinetStatus("2");
        //离柜盒数
        Integer outBoxNum = arcArchiveStatisticseMapper.setboxNumCabinetStatus("1");
        // 查询未借阅档案盒数
        Integer archiveLibrary = arcArchiveStatisticseMapper.getBoxBorrowflag("2");
        //查询借阅中档案数
        Integer archiveBorrow = arcArchiveStatisticseMapper.getBoxBorrowflag("1");
        //遗失
        Integer perditaNum = arcArchiveStatisticseMapper.getPerditaNum();
        //违规带出
        List<Map<String, ArcAlarm>> maps = arcArchiveStatisticseMapper.selectListDescLimitTwo();
        Integer BorrowOut = maps.size();

        map.put("archiveNum", archiveNum);
        map.put("boxNum", boxNum);
        map.put("inBoxNum", inBoxNum);
        map.put("outBoxNum", outBoxNum);
        map.put("archiveLibrary", archiveLibrary);
        map.put("archiveBorrow", archiveBorrow);
        map.put("perditaNum", perditaNum);
        map.put("BorrowOut", BorrowOut);
        return map;
    }


}
